/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commons.lang.StrToken;

//import java.util.StringTokenizer;

import org.apache.commons.lang.text.StrMatcher;
import org.apache.commons.lang.text.StrTokenizer;


/**
 * 分割字符串
 * @author WangShuai
 */
public class Demo1 {

    public static void main(String[] args) {
        //1.JDK中的StringTokenizer Class
//        StringTokenizer st = new StringTokenizer("this is a test");
        //The tokenizer uses the default delimiter set, which is " \t\n\r\f":
        //the space character, the tab character, the newline character, the carriage-return character, and the form-feed character(换页符).
//        while (st.hasMoreTokens()) {
//            System.out.println(st.nextToken());
//        }

        //its use is discouraged in new code  AND  use the split method of String or the java.util.regex package instead.
        //split instead
//        String[] result = "this is a test".split("\\s");
//        for (int x = 0; x < result.length; x++) {
//            System.out.println(result[x]);
//        }

        //2.Commons Lang
        //StrTokenizer stoken = new StrTokenizer();  //This constructor is normally used with reset(String).
        //StrTokenizer stoken = new StrTokenizer("this is a test");  //splitting on space, tab, newline and formfeed as per StringTokenizer.
        //StrTokenizer stoken = new StrTokenizer("this--is--a--test","--"); //splitting on the specified character.eg:--

        //A matcher class that can be queried to determine if a character array portion matches.
        StrMatcher matcher1 = StrMatcher.charSetMatcher("-");
        StrMatcher matcher2 = StrMatcher.trimMatcher();

        StrMatcher matcher3 = StrMatcher.spaceMatcher();
        StrMatcher matcher4 = StrMatcher.quoteMatcher();
        StrMatcher matcher5 = StrMatcher.charSetMatcher(";-");  // Can set mutil delimiter char  // /n is new line sperate char
        StrTokenizer stoken = new StrTokenizer("this-is-a-test- s p a c e;that-is-a-space",matcher1);
        stoken.setTrimmerMatcher(matcher2);
        stoken.setIgnoredMatcher(matcher3);
        stoken.setDelimiterMatcher(matcher5); //Only the last delimiter matcher can use
        
        stoken = new StrTokenizer("That's is \"CHINA\" and \"USA\"");
        stoken.setQuoteMatcher(matcher4);
        while (stoken.hasNext()) {
            System.out.println(stoken.nextToken());
        }

    }
}
